﻿/*****************************************************************************/
/* Function :: CanViewGrant													 */
/*****************************************************************************/

CREATE FUNCTION [GrantManagement].[fnCanViewGrant]
(
	@GrantID BIGINT,
	@VolunteerID BIGINT
)
RETURNS bit
AS
BEGIN

	-- GrantManagers can only view grants they own.
	IF (EXISTS (SELECT * FROM UserRoles WHERE ((UserID = @VolunteerID) AND (Role = 'GrantManagers'))))
	BEGIN
		IF ((SELECT CaseVolunteerID FROM Cases
			 INNER JOIN Grants ON Grants.CaseID = Cases.OID
			 WHERE (Grants.OID = @GrantID)) = @VolunteerID)
			RETURN 1
		RETURN 0
	END

	RETURN 1   -- Everyone else can see all grants.

END
